<template>
  <div class="login-index">
    <div class="login-box">
      <el-row class="row-flex">
        <el-col :span="12">
          <img style="width: 100%" :src="require('@/assets/login_left1.png')" />
        </el-col>
        <el-col :span="12">
          <div class="content">
            <div class="login-content">
              <div class="title">IBEM门户后台管理系统</div>
              <div>
                <el-form
                  ref="formRef"
                  :model="ruleForm"
                  :rules="rules"
                  class="demo-ruleForm"
                >
                  <el-form-item prop="user" style="margin-bottom: 20px">
                    <el-input
                      v-model="ruleForm.user"
                      prefix-icon="User"
                      maxlength="50"
                      placeholder="用户名"
                      class="myClass-login"
                    />
                  </el-form-item>
                  <el-form-item
                    prop="passwd"
                    style="margin-bottom: 25px"
                    class="myClass-login"
                  >
                    <el-input
                      v-model="ruleForm.passwd"
                      prefix-icon="Lock"
                      type="password"
                      maxlength="20"
                      placeholder="密码"
                      show-password
                      @keyup.enter.native="submitForm('formRef')"
                    />
                  </el-form-item>
                  <el-form-item>
                    <!-- <el-checkbox label="同意并接受用户协议与隐私政策"></el-checkbox> -->
                    <!--<div class="forget-pwd" @click="forgetPwdFun">忘记密码</div>-->
                  </el-form-item>
                  <el-form-item>
                    <div class="btn-group">
                      <el-button class="btn" @click="resetForm('formRef')"
                        >重置</el-button
                      >
                      <el-button type="primary" class="btn" @click="submitForm"
                        >登录</el-button
                      >
                    </div>
                  </el-form-item>
                </el-form>
              </div>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import { defineComponent, onMounted, reactive, toRefs } from "vue";
import { useUserStore } from "@/stores/user";
import { ElMessage } from "element-plus";
const userStore = useUserStore();
export default defineComponent({
  name: "Login",
  setup() {
    const state = reactive({
      ruleForm: {
        user: "",
        passwd: "",
      },
      formRef: null,
      rules: {
        user: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        passwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      checked: true,
      btnLoading: false,
      captchaImg: "",
    });
    onMounted(() => {
      window.addEventListener("keydown", keyDown, true);
    });
    const keyDown = (e) => {
      //如果是回车则执行登录方法
      if (e.keyCode == 13) {
        submitForm("formRef");
      }
    };
    const submitForm = () => {
      if (state.ruleForm.user !== 'admin') {
        ElMessage.error("此账号没有登录权限！");
        return;
      }
      state.btnLoading = true;
      state.formRef.validate((valid) => {
        if (valid) {
          state.btnLoading = false;

          // 如果验证规则通过，则调用 userStore 中的 getUserInfo 函数
          userStore.getUserInfo(state.ruleForm);
          // 用户信息就会存在pinia里面
        } else {
          state.btnLoading = false;
          //console.log("error submit!!");
          return false;
        }
      });
    };
    const resetForm = () => {
      if (!state.formRef) return;
      state.formRef.resetFields();
    };
    return {
      ...toRefs(state),
      keyDown,
      submitForm,
      resetForm,
    };
  },
});
</script>

<style lang="scss">
.login-index {
  height: 100%;
  min-height: 550px;
  background-color: #eeeeee;
  background-image: url("@/assets/login_bg.svg");
  background-size: 100% 100%;
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  .login-box {
    position: relative;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 96.5%;
    height: 94%;
    padding: 0 50px;
    background-color: rgb(255 255 255 / 80%);
    border-radius: 10px;
    margin: auto;
    top: 3%;
  }
  .el-input__wrapper {
    border-radius: 8px;
  }
  .el-input__icon {
    font-size: 20px !important;
  }
  .el-input__inner {
    border: none;
    border-radius: 10px;
    font-size: 18px !important;
    padding-left: 0px;
    height: 40px;
  }
  .row-flex {
    height: 100%;
    align-items: center;
    vertical-align: middle;
    align-content: center;
  }
  .content {
    margin: auto;
    padding: 20px;
    box-shadow: 0px 0px 10px #a3a2a2;
    width: 450px;
    background-color: #fff;
    border-radius: 10px;
    flex: 1;
    .logo {
      display: flex;
      align-items: center;

      img {
        margin: auto;
      }
    }
    .login-content {
      width: 380px;
      margin: auto;
      .title {
        font-family: "Microsoft YaHei";
        font-style: normal;
        font-weight: 700;
        font-size: 30px;
        line-height: 100px;
        color: #000000;
      }
      .btn-group {
        width: 100%;
        display: flex;
        justify-content: space-between;
        .btn {
          border-radius: 40px;
          width: 45%;
          height: 40px;
        }
      }
    }
  }
}
</style>
